package com.crm.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 前端响应返回结果类
 *
 * @author wb
 * @since 2023-12-10 17:43
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    private Integer code;
    private String message;
    private T data;


    public static <T> Result<T> success(){
        return success(null);
    }
    public static <T> Result<T> success(T data) {
        return build(data, ResultEnum.SUCCESS);
    }

    public static <T> Result<T> failure(){
        return failure(ResultEnum.FAILURE);
    }

    public static <T> Result<T> failure(T data){
        return build(data, ResultEnum.FAILURE);
    }

    public static <T> Result<T> failure(ResultEnum resultEnum) {
        return build(null, resultEnum);
    }


    private static <T> Result<T> build(T data) {
        Result<T> result = new Result<>();
        if(null != data) {
            result.setData(data);
        }
        return result;
    }

    private static <T> Result<T> build(T data, ResultEnum resultEnum) {
        Result<T> result = build(data);
        result.setCode(resultEnum.getCode());
        result.setMessage(resultEnum.getMessage());
        return result;
    }

}